package com.chhuang.huaao.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chhuang.core.bean.QueryBean;
import com.chhuang.core.service.impl.BaseServiceImpl;
import com.chhuang.huaao.bean.HaFeedbackQueryBean;
import com.chhuang.huaao.mapper.HaFeedbackMapper;
import com.chhuang.huaao.po.HaFeedback;
import com.chhuang.huaao.po.HaFeedbackExample;
import com.chhuang.huaao.service.IHaFeedbackService;
import com.chhuang.utils.Assert;

/**
* HaFeedback ServiceImpl
* @author ChHuang
* @date Sun Apr 30 20:19:00 CST 2017
*/
@Service
public class HaFeedbackServiceImpl extends BaseServiceImpl<HaFeedbackMapper, HaFeedback> implements IHaFeedbackService {
	@Autowired
	private HaFeedbackMapper haFeedbackMapper;

	@Override
	public QueryBean<HaFeedback> seniorQuery(QueryBean<HaFeedback> queryBean) throws Exception {
		HaFeedbackQueryBean poQueryBean = (HaFeedbackQueryBean)queryBean;
		HaFeedbackExample example = new HaFeedbackExample();
		
		if(poQueryBean.isAnd()){//全部是与条件
			HaFeedbackExample.Criteria criteria = example.createCriteria();
			if(Assert.isValidString(poQueryBean.getFeedbackId())){
				criteria.andFeedbackIdEqualTo(poQueryBean.getFeedbackId());
			}
			if(Assert.isValidString(poQueryBean.getFeedbackContent())){
				criteria.andFeedbackContentLike("%"+poQueryBean.getFeedbackContent()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerId())){
				criteria.andCustomerIdLike("%"+poQueryBean.getCustomerId()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerAccount())){
				criteria.andCustomerAccountLike("%"+poQueryBean.getCustomerAccount()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerName())){
				criteria.andCustomerNameLike("%"+poQueryBean.getCustomerName()+"%");
			}
			if(Assert.isValidString(poQueryBean.getDeviceModel())){
				criteria.andDeviceModelLike("%"+poQueryBean.getDeviceModel()+"%");
			}
			if(Assert.isValidString(poQueryBean.getDeviceVendor())){
				criteria.andDeviceVendorLike("%"+poQueryBean.getDeviceVendor()+"%");
			}
			if(Assert.isValidString(poQueryBean.getDeviceSystem())){
				criteria.andDeviceSystemLike("%"+poQueryBean.getDeviceSystem()+"%");
			}
			if(poQueryBean.getCreateTime() != null){
				criteria.andCreateTimeEqualTo(poQueryBean.getCreateTime());
			}
		}else{//全部是或条件
			if(Assert.isValidString(poQueryBean.getFeedbackId())){
				example.or().andFeedbackIdEqualTo(poQueryBean.getFeedbackId());
			}
			if(Assert.isValidString(poQueryBean.getFeedbackContent())){
				example.or().andFeedbackContentLike("%"+poQueryBean.getFeedbackContent()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerId())){
				example.or().andCustomerIdLike("%"+poQueryBean.getCustomerId()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerAccount())){
				example.or().andCustomerAccountLike("%"+poQueryBean.getCustomerAccount()+"%");
			}
			if(Assert.isValidString(poQueryBean.getCustomerName())){
				example.or().andCustomerNameLike("%"+poQueryBean.getCustomerName()+"%");
			}
			if(Assert.isValidString(poQueryBean.getDeviceModel())){
				example.or().andDeviceModelLike("%"+poQueryBean.getDeviceModel()+"%");
			}
			if(Assert.isValidString(poQueryBean.getDeviceVendor())){
				example.or().andDeviceVendorLike("%"+poQueryBean.getDeviceVendor()+"%");
			}
			if(Assert.isValidString(poQueryBean.getDeviceSystem())){
				example.or().andDeviceSystemLike("%"+poQueryBean.getDeviceSystem()+"%");
			}
			if(poQueryBean.getCreateTime() != null){
				example.or().andCreateTimeEqualTo(poQueryBean.getCreateTime());
			}
		}
		
		poQueryBean.setItemCount(haFeedbackMapper.countByExample(example));//总记录数
		if(poQueryBean.getItemCount()>0){//计算总页数
			poQueryBean.setPageCount(poQueryBean.getItemCount()/poQueryBean.getPageSize()+
					(int)(poQueryBean.getItemCount()%poQueryBean.getPageSize()>0?1:0));
			if(poQueryBean.getPageCount()>=poQueryBean.getPageNum()){
				if(Assert.isValidString(poQueryBean.getOrderBy())){//排序
					example.setOrderByClause(poQueryBean.getOrderBy()+" "+poQueryBean.getOrder());
				}
				//分页
				example.setOffset(poQueryBean.getOffset());
				example.setLimit(poQueryBean.getLimit());
				poQueryBean.setResult(haFeedbackMapper.selectByExample(example));
			}
		}
		
		return poQueryBean;
	}

	@Override
	public void deleteByPrimaryKeys(List<String> list) throws Exception {
		HaFeedbackExample example = new HaFeedbackExample();
		HaFeedbackExample.Criteria criteria = example.createCriteria();
		criteria.andFeedbackIdIn(list);
		haFeedbackMapper.deleteByExample(example);
	}
}